package com.jinduplan.planmanager.ui.activity.clockin

import android.graphics.PorterDuff
import android.text.format.DateUtils
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.cxz.kotlin.baselibs.adapter.CustomRecyclerAdapter
import com.cxz.kotlin.baselibs.adapter.ViewHolder
import com.cxz.kotlin.baselibs.base.BaseMvpTitleActivity
import com.cxz.kotlin.baselibs.base.changeDrawalbeColor
import com.cxz.kotlin.baselibs.base.getCombineStr
import com.jinduplan.planmanager.R
import com.jinduplan.planmanager.bean.ClockinDetailData
import com.jinduplan.planmanager.bean.ClockinListData
import com.jinduplan.planmanager.bean.ClockinRecord
import com.jinduplan.planmanager.bean.ClockinRecordData
import com.jinduplan.planmanager.mvp.contract.ClockinRecordContract
import com.jinduplan.planmanager.mvp.presenter.ClockinRecordPresenter
import com.jinduplan.planmanager.utils.ResUtil
import com.jinduplan.planmanager.utils.StatusBarUtil
import kotlinx.android.synthetic.main.activity_rcy_refresh.*

class ClockinRecordActivity:BaseMvpTitleActivity<ClockinRecordContract.View,ClockinRecordContract.Presenter>(),ClockinRecordContract.View,SwipeRefreshLayout.OnRefreshListener {

    val datas = ArrayList<ClockinListData>()
    override fun attachChildLayoutRes(): Int {
        return R.layout.activity_rcy_refresh
    }

    override fun createPresenter(): ClockinRecordContract.Presenter {
        return ClockinRecordPresenter()
    }

    override fun start() {
        setStatusBarColor(ResUtil.getC(R.color.bg_black_jdy))
        com.cxz.kotlin.baselibs.utils.StatusBarUtil.setDarkMode(this)
        setToolbarColor(R.color.bg_black_jdy)
        setBaseTitleColor(R.color.white)
        setBaseTitle("打卡记录")
        val upArrow = R.mipmap.ruturn_s.changeDrawalbeColor(this,R.color.white)
        getSupportActionBar()?.setHomeAsUpIndicator(upArrow)
//        val up = ContextCompat.getDrawable(this,R.drawable.ic_icon_aqi);
//
//        if (up != null) {
//
//            val drawableUp = DrawableCompat.wrap(up)
//
//            DrawableCompat.setTint(drawableUp, ContextCompat.getColor(context!!, aqiIconColor))
//
//            cityWeatherBinding?.ivAirNum?.setImageDrawable(drawableUp)
//
//        }


//        swipeRefreshLayout.setBackgroundColor(R.color.bg_black_jdy)
        recyclerView.setBackgroundColor(ResUtil.getC(R.color.bg_black_jdy))
        getBaseContainer().setBackgroundColor(ResUtil.getC(R.color.bg_black_jdy))

        recyclerView.adapter = object :
            CustomRecyclerAdapter<ClockinListData>(this, R.layout.item_clockin_record, datas) {
            override fun convert(holder: ViewHolder, t: ClockinListData, position: Int) {
                val rcyClockinDetail = holder.getView<RecyclerView>(R.id.rcyClockinDetail)
                if (null == rcyClockinDetail.adapter) {
                    rcyClockinDetail.adapter = object : CustomRecyclerAdapter<ClockinDetailData>(
                        this@ClockinRecordActivity,R.layout.item_clock_in,t.detaiList) {
                        override fun convert(
                            holder: ViewHolder,
                            t: ClockinDetailData,
                            position: Int
                        ) {
                            holder.setText(R.id.tvClockInTime, t.time)
                            holder.setText(R.id.tvClockInLocation, t.addr)

                        }
                    }
                    rcyClockinDetail.layoutManager = LinearLayoutManager(this@ClockinRecordActivity)
                } else {
                    rcyClockinDetail.adapter?.notifyDataSetChanged()
                }
                holder.setText(R.id.tvDate,t.date)

            }
        }
        recyclerView.layoutManager = LinearLayoutManager(this)
        mPresenter?.refreshClockinRecord("1","100")
        swipeRefreshLayout.setOnRefreshListener(this)

    }

    override fun refreshClockinRecordResult(record: ClockinRecord?) {
        needShowLoading = true
        swipeRefreshLayout.isRefreshing = false
        record?.data?.let {
            datas.clear()
            var recordStr= ""
            var index = -1
            it.forEach { bean->
                val date = bean.created_at.split(" ")[0].replace("-","/")
                val time = bean.created_at.split(" ")[1]

                var addr = ""
                bean.project?.let {
                    if (!it.project_addr.isNullOrEmpty()) {
                        addr = "$addr${it.project_addr}"
                        if (!it.project_name.isNullOrEmpty()) {
                            addr = "$addr${it.project_name}"
                        }
                    }
                }
                bean.building?.let {
                    it.building_name?.let {
                        addr = "$addr$it"
                    }
                }
                if (!recordStr.contains(date)) {
                    index++
                    recordStr = recordStr.getCombineStr(date, "##")
                    datas.add(ClockinListData(date, ArrayList()))
                    datas[index].detaiList.add(ClockinDetailData(time,addr))
                } else {
                    datas[index].detaiList.add(ClockinDetailData(time,addr))
                }
            }
            recyclerView.adapter?.notifyDataSetChanged()
        }

    }

    override fun loadClockinRecordResult(record: ClockinRecord?) {
        record?.data?.let {

            var recordStr= ""
            var index = -1
            it.forEach { bean->
                val date = bean.created_at.split(" ")[0].replace("-","/")
                val time = bean.created_at.split(" ")[1]
                val addr = "${if (bean.project.project_addr.isNullOrEmpty()) "" else bean.project.project_addr}${bean.project.project_name}"
                if (!recordStr.contains(date)) {
                    index++
                    recordStr = recordStr.getCombineStr(date, "##")
                    datas.add(ClockinListData(date, ArrayList()))
                    datas[index].detaiList.add(ClockinDetailData(time,addr))
                } else {
                    datas[index].detaiList.add(ClockinDetailData(time,addr))
                }
            }
            recyclerView.adapter?.notifyDataSetChanged()
        }
    }

    override fun onRefresh() {
        needShowLoading = false
        mPresenter?.refreshClockinRecord("1","100")
    }
}